<template>
  <div  style="padding: 15px;">
    <vxe-toolbar id="warehouseLocationId" :custom="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="findList">查询</el-button>
        <el-button @click="funcAdd">新增</el-button>
        <el-button @click="start">启用</el-button>
        <el-button @click="block">停用</el-button>
        <el-button>导入</el-button>

      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      show-footer
      highlight-hover-row
      row-id="id"
      class="mytable-footer"
      height="700"
       :loading="loading"
      :data="tableData"
      :edit-config="{trigger: 'click', mode: 'row', showStatus: true}"
      sort-config.remote
      @sort-change="sortList"
    >
      <vxe-table-column type="checkbox" width="60"></vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="status" title="状态" min-width="100" sortable show-header-overflow show-overflow/>
        <template v-slot:header="{ column }">
          <el-select v-model="listQuery.status" placeholder="请选择状态" clearable>
            <el-option value="0" label="启用"/>
            <el-option value="1" label="停用"/>
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="serialNumber" title="容器编码" min-width="100" sortable />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.serialNumber" placeholder="请输入容器编码" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="name" title="容器分类" min-width="100" sortable />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.name" placeholder="请输入容器分类" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="type" title="占用状态" min-width="100" sortable />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.type" placeholder="请输入占用状态" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="createdBy" title="创建人" min-width="100" sortable />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.createdBy" placeholder="请输入创建人" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="gmtCreated" title="创建时间" min-width="100" sortable />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.gmtCreated" placeholder="请输入创建时间" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="modifiedBy" title="修改人" min-width="100" sortable />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.modifiedBy" placeholder="请输入修改人" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="gmtModified" title="修改时间" min-width="100" sortable />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.gmtModified" placeholder="请输入修改时间" clearable />
        </template>
      </vxe-table-column>

      <!-- <vxe-table-column>
        <vxe-table-column field="warehouseCode" :formatter="formatterStatus" title="所属仓库" min-width="100" sortable />
        <template v-slot:header="{ column }">
          <el-select v-model="listQuery.warehouseCode" placeholder="请选择所属仓库" @change="findwarehouseSpaceList" style="width: 100%">
            <el-option v-for="item in warehouseList" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
        </template>
      </vxe-table-column> -->
      <vxe-table-column title="操作" fixed="right" min-width="150">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="详情" placement="top">
            <vxe-button type="text" @click="funcDetail(scope.row.code)">
              <i class="el-icon-document" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="编辑" placement="top">
            <vxe-button type="text" @click="funcEdit(scope.row.code)">
              <i class="el-icon-edit" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top">
            <vxe-button type="text" @click="funcDelete(scope.row.code)">
              <i class="el-icon-delete" />
            </vxe-button>
          </el-tooltip>

        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />

    <vxe-modal v-model="modelAddFlag" title="新增" width="1100" height="700" resize>
      <compAdd ref="addRef" @fatherAddMethod="fatherAdd" @fatherCancelMethod="fatherCancel" />
    </vxe-modal>
    <vxe-modal v-model="modelEditFlag" title="编辑" width="1100" height="700" resize>
      <compEdit ref="editRef" @fatherEditMethod="fatherEdit" @fatherCancelMethod="fatherCancel" />
    </vxe-modal>
    <vxe-modal v-model="modelDetailFlag" title="详情" width="1100" height="700" resize>
      <compDetail ref="detailRef" />
    </vxe-modal>

  </div>
</template>
<script>
import Vue from 'vue'
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/index.css'
import XEUtils from 'xe-utils'
import VXEUtils from 'vxe-utils'
Vue.use(VXETable, VXEUtils, XEUtils, { mounts: ['cookie'] })
import compAdd from './components/WarehouseLocationAdd.vue'
import compEdit from './components/WarehouseLocationEdit.vue'
import compDetail from './components/WarehouseLocationDetail.vue'
import request from '@/utils/request'
import { warehouseList } from '@/api/basicInfo'

export default {
  name: 'Parent',
  components: {
    compAdd,
    compEdit,
    compDetail,
  },
  data() {
    return {
      message: '',
      loading: false,
      tableData: [],
      modelAddFlag: false,
      modelbindingFlag:false,
      modelEditFlag: false,
      modelDetailFlag: false,
      copy: false,
      listQuery: {
        address:'',        //地址 
        city:'',           //市 
        code:'',           //数据标识 
        contacts:'',       //联系人 
        country:'',        //国家 
        county:'',         //县/区 
        email:'',          //邮箱 
        fax:'',            //传真 
        groupCode:'',      //所属组织 
        landlineNumber:'', //联系人号码 
        latitude:'',       //维度 
        longitude:'',      //经度 
        name:'',           //仓库名称 
        phoneNumber:'',    //手机号码 
        province:'',       //省 
        remarks:'',        //备注 
        serialNumber:'',   //仓库代码 
        createdBy:'',      //创建人 
        modifiedBy:'',     //修改人 
        gmtCreated:'',     //创建时间
        gmtModified:'',    //修改时间 

        status:'',         //状态 
        total: 0,          //总数 
        type:'',           //仓库类型
        pageNum: 1,
        pageSize: 10,
        sortField: '',
        sortType: '',
      },
      // 所属仓库
      warehouseList: [],
      // 所属库区
      warehouseSpaceList: [],
      // 新所属库区
      warehouseSpaceListNew: []
    }
  },
  created() {
    this.findList()
  },
  methods: {
    //启用
    start(){
      let selectRecords = this.$refs.xTable.getCheckboxRecords()
      alert('启用')
    },
    //停用
    block(){
      let selectRecords = this.$refs.xTable.getCheckboxRecords()
      alert('停用')
    },
    //绑定入驻商
    binding(){
      // this.$refs.bindingRef.getInfo(code)
      this.modelbindingFlag = true
    },
    //获取列表
    findList() {
      this.loading = true
      warehouseList(this.listQuery).then(response => {
        this.tableData = response.data.items
        this.listQuery.total = response.data.total
        this.loading = false
      })
    },
    // 查询仓库
    findWarehouseList() {

      request({ url: `/ntocc-wms-api/warehouse/chooseData`, method: 'get', params: { "pageSize":10000 }}).then(response => {
        this.warehouseList = response.data
      })
    },
    // 查询库区
    findwarehouseSpaceList() {
      request({ url: `/ntocc-wms-api/warehouseSpace`, method: 'get', params: { 'warehouseCode': this.listQuery.warehouseCode,"pageSize":10000 }}).then(response => {
        this.warehouseSpaceList = response.data.records
      }).then(response => {
        this.findList()
      })
    },
    // 查询库区
    findwarehouseSpaceListNew() {
      request({ url: `/ntocc-wms-api/warehouseSpace`, method: 'get' }).then(response => {
        this.warehouseSpaceListNew = response.data.records
      }).then(response => {
        this.findList()
      })
    },
    // 翻译仓库名称
    formatterStatus({ cellValue }) {
      const item = this.warehouseList.find(item => item.code == cellValue)
      return item ? item.name : ''
    },
    // 翻译仓库区域
    formatterSpace({ cellValue }) {
      const item = this.warehouseSpaceList.find(item => item.code == cellValue)
      return item ? item.name : ''
    },
    findList() {
      this.loading = true
      return new Promise(resolve => {
        request({
          url: '/ntocc-wms-api/warehouseLocation',
          method: 'get',
          params: this.listQuery
        }).then(response => {
          this.tableData = response.data.records
          this.listQuery.total = response.data.total
          this.loading = false
          resolve();
        })
      })
    },

    // 排序
    sortList({ column, property, order }) {
      if (order) {
        this.listQuery.sortField = property
        this.listQuery.sortType = order
      } else {
        this.listQuery.sortField = ''
      }
      this.findList()
    },
    // 添加方法
    funcAdd() {
      this.$refs.addRef.reset();    //重置

      this.modelAddFlag = true
    },
    // 修改方法
    funcEdit(code) {
      this.$refs.editRef.getInfo(code)
      this.modelEditFlag = true
    },
    // 详情方法
    funcDetail(code) {
      this.$refs.detailRef.getInfo(code)
      this.modelDetailFlag = true
    },
    // 删除方法
    funcDelete(row) {
      this.$confirm('确认删除吗？').then(_ => {
        request({ url: `/ntocc-wms-api/warehouseLocation`, method: 'delete', params: { codes: row }}).then(response => {
          this.$message({ message: response.msg, type: 'success' })
          this.findList()
        })
      }).catch(_ => {})
    },
    // 增加组件的父方法
    fatherAdd() {
      this.modelAddFlag = false
      this.findList()
    },
    // 编辑组件的父方法
    fatherEdit() {
      this.modelEditFlag = false
      this.findList()
    },
    // 公共取消
    fatherCancel(modelFlag) {
      switch (modelFlag) {
        case 'add':
          this.modelAddFlag = false
          break
        case 'edit':
          this.modelEditFlag = false
          break
        case 'detail':
          this.modelDetailFlag = false
          break
        default:
      }
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage
      this.listQuery.pageSize = pageSize
      this.findList()
    },
    // 底部颜色设置
   /*  footerCellClassName({ $rowIndex, column, columnIndex }) {
      if (columnIndex === 0) {
        if ($rowIndex === 0) {
          return 'col-blue'
        } else {
          return 'col-red'
        }
      }
    }, */
    // 设置底部合计
    footerMethod({ columns, data }) {
      return [
        columns.map((column, columnIndex) => {
          if (columnIndex === 0) {
            return '合计'
          }
          if (['pageviews', 'forecast'].includes(column.property)) {
            return XEUtils.sum(data, column.property)
          }
          return null
        })
      ]
    }
  }
}
</script>

<style>
.vxe-table .vxe-body--column:not(.col--ellipsis), .vxe-table .vxe-footer--column:not(.col--ellipsis), .vxe-table .vxe-header--column:not(.col--ellipsis) {
 padding: 6px 0;
}
.vxe-toolbar{padding:0 0 10px 0}
</style>
